<?php
/**
* 
*/
class MY_Model extends CI_Model
{
	public $mytable = "";
	public $primaryKey = "";
	public $resCode = "";

	function __construct() {
		parent::__construct();
	}

	function field() {
		$fieldData = array();
		return $fieldData;
	}

	function get($val = '', $param = null) {
		if ($param == null)
			$param = $this->primaryKey;

		if (is_array($val) && $val != array())
			$this->db->where($val);
		else if ($val != '') 
			$this->db->where($param, $val);
			
		$this->db->from($this->mytable);
		return $this->db->get()->first_row();
	}

	function gets($val = '', $param = null) {
		if ($param == null)
			$param = $this->primaryKey;
		if (is_array($val) && $val != array())
			$this->db->where($val);
		else if ($val != '')
			$this->db->where($param, $val);

		$this->db->from($this->mytable);
		return $this->db->get()->result();
	}

	function pagination($val = '', $param = null, $config = array()) {
		if ($param == null)
			$param = $this->primaryKey;
		if (is_array($val) && $val != array())
			$this->db->where($val);
		else if ($val != '')
			$this->db->where($param, $val);
		if (isset($config['cur_page']) && isset($config['per_page']))
			$this->db->limit($config['per_page'], ($config['cur_page'] - 1) * $config['per_page']);
		$this->db->from($this->mytable);
		return $this->db->get()->result();
	}

	function where_in($val = '', $param = null) {
		if ($param == null)
			$param = $this->primaryKey;

		$this->db->from($this->mytable);
		if ($val != '')
			$this->db->where_in($this->mytable . "." . $param, $val);
		return $this->db->get()->result();
	}

	function num($val = '', $param = null) {
		if ($param == null)
			$param = $this->primaryKey;

		$this->db->from($this->mytable);
		if ($val != '')
			$this->db->where($param, $val);
		return $this->db->count_all_results();
	}

	function update($val = '', $param = null, $arr_update) {
		if ($param == null)
			$param = $this->primaryKey;

		if ($val != '')
			$this->db->where($param, $val);
		$arr_update = $this->update_security($arr_update,$this->field());
		$this->db->update($this->mytable, $arr_update);
	}

	function insert($arr_insert) {
		$arr_insert = $this->insert_security($arr_insert,$this->field());
		$this->db->insert($this->mytable, $arr_insert);
		return $this->db->insert_id();
	}

	function delete($val = '', $param = null) {
		if ($val != '') {
			if ($param == null)
				$param = $this->primaryKey;
			if (is_array($val) && $val != array())
				$this->db->where($val);
			else if ($val != '')
				$this->db->where($param, $val);
			$this->db->where($param, $val);
			$this->db->delete($this->mytable);
		}
	}

	function key_value($data, $value, $key = '') {
		if (!$key) {
			$key = $this->primaryKey;
		}
		$result = array();
		if ($data) foreach ($data as $item) {
			if (is_array($item)) $item = (object) $item;
			@$result[$item->$key] = @$item->$value;
		}
		return $result;
	}
	
	function data_field($data, $field_wana_get_data = '') {
		if (!$field_wana_get_data) {
			$field_wana_get_data = $this->primaryKey;
		}
		$result = array();
		if ($data) foreach ($data as $item) {
			if (is_array($item)) $item = (object) $item;
			$result[] = $item->$field_wana_get_data;
		}
		return $result;
	}
	
	function search(){
		$post = $this->input->post();
		$data = array();
		if($post){
			$s = $this->input->post('txtSearch');
			$field = $this->input->post('selFieldSearch');
			$condition = $this->input->post('selCondition');
			if(trim($s) !='')
			setcookie("search",$field."|".$s.'|'.$condition, time() + 7200);
			redirect($this->url_index);
		}
		else if($this->input->cookie('search')) {
			$str = $this->input->cookie('search');
			$str = explode("|",$str);
			if(count($str) >2){
				$field = $str[0];
				$s = $str[1];
				$condition = $str[2];
			}
			else {
				return $data;
			}
		}
		if(isset($s)){
			if($condition == '='){
				$this->db->where($field,$s);
				return $data;
			}
			else {
				$this->db->like($field,$s);
				return $data;
			}
			
		}
		
		return $data;
	}

	function array_same_key($array1 = array(), $array2 = array()) {
        return array_intersect_key($array1, $array2);
    }

    function insert_security($array_insert, $array_default = array()) {
        $arr_temp = array_merge($array_default, $array_insert);
        return $this->array_same_key($arr_temp, $array_default);
    }

    function update_security($array_insert, $array_default = array()) {
        return $this->array_same_key($array_insert, $array_default);
    }
}
?>